package com.glory.dmegcpda.viewmodel

import com.glory.dmegcpda.adapter.CheckInfoAdapter
import com.glory.dmegcpda.bean.CheckInfoEntity
import com.glory.dmegcpda.model.CheckRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils

/**
 * FileName: CheckInfoViewModel
 * Author: XJH
 * Date: 2021/10/21 16:18
 * Description:
 * History:
 */
class CheckInfoViewModel : BaseViewModel() {

    val checkList = arrayListOf<CheckInfoEntity.DataInfoEntity>()
    val mAdapter : CheckInfoAdapter by lazy { CheckInfoAdapter(checkList) }
    val checkRepository by lazy { CheckRepository() }


    fun init(sheetRRN : String){
        val map = JsonObject()
        map.addProperty("ENTITYMODEL","com.glory.pms.sheet.model.SheetCheckTableLineData")
        map.addProperty("WHERECLAUSE","sheetDataRrn='$sheetRRN'")
        launchOnlyResult({
            val checkInfo = checkRepository.getCheckInfo(map)
            checkList.addAll(checkInfo.DATALIST)
            mAdapter.notifyDataSetChanged()
        })
    }

    fun save(checkPerson : String,isTemp : String,sheetRRN : String){
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","FORM")
        map.addProperty("SCHEDULERRN",sheetRRN)
        map.addProperty("ISFINISHED",isTemp)
        map.addProperty("ISFINISHED",isTemp)
        map.addProperty("OPERATOR",checkPerson)
        mAdapter.data.forEach {
            it.CHECKUSER = checkPerson
            it.OPERATOR = checkPerson
            it.CHECKTIME = FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss")
            it.CONFIRMTIME = FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss")
        }
        map.add(
            "CHECKLINELIST",
            JsonParser().parse(Gson().toJson(mAdapter.data)).asJsonArray
        )
        launchOnlyResult({
            checkRepository.saveCheckInfo(map)
            defUI.toastEvent.value = ToastMessageBean("保存成功",true)
            defUI.startActivity.call()
        })
    }

}